package com.qf.java2105.huangchuang.hotel.controller.food;

import com.qf.java2105.huangchuang.hotel.entity.ResultVO;
import com.qf.java2105.huangchuang.hotel.factory.BeanFactory;
import com.qf.java2105.huangchuang.hotel.pojo.Food;
import com.qf.java2105.huangchuang.hotel.service.IFoodService;
import com.qf.java2105.huangchuang.hotel.service.impl.FoodServiceImpl;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 菜品控制层
 * @author hcvg
 * @version v1.0
 * @date 2021/09/12
 **/
@WebServlet("/food/search")
public class FoodSearchController extends HttpServlet {
    IFoodService foodService = (IFoodService) BeanFactory.getBean("foodService");
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String keyword = request.getParameter("keyword");
        ResultVO<List<Food>> resultVO = foodService.findByFoodName(keyword);

        request.setAttribute("keyword", keyword == null ? "" : keyword.trim());
        request.setAttribute("foods", resultVO.getData());
        request.getRequestDispatcher(request.getContextPath() + "/backend/detail/food/food-list.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
